/**
 * 
 */
package edu.umd.clip.lm.tools;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

import edu.umd.clip.lm.factors.*;
import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.util.*;

import edu.berkeley.nlp.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class BuildOvertVocabularies {

	public static class Options {
        @Option(name = "-input", required = false, usage = "Training data file (Default: stdin)")
		public String input;
        @Option(name = "-config", required = true, usage = "XML config file")
		public String config;
        @Option(name = "-debug", usage = "Print current overt factors and exit")
		public boolean debug = false;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);

		Experiment.initialize(opts.config);
		Experiment experiment = Experiment.getInstance();
		
		
		if (opts.debug) {
			Map<FactorTuple, FactorTuple> allOvertFactors = experiment.getTupleDescription().getAllOvertFactors();
			for(FactorTuple overtFactors : allOvertFactors.keySet()) {
				System.out.println(overtFactors.toStringNoNull());
			}
			System.exit(1);
		}
		
		final InputParser parser = experiment.getTupleParser();
		{
			long[] endTuples = new long[1];
			endTuples[0] = experiment.getTupleDescription().createEndTuple(); 
			parser.setEndTuples(endTuples);
		}

		for(byte i = 0; i < experiment.getTupleDescription().numFactors(); ++i) {
			FactorDescription desc = experiment.getTupleDescription().getDescription(i);
			if (!desc.isOvert() || i == experiment.getTupleDescription().getMainFactorIndex()) continue;
			// clear dictionaries
			desc.setDictionary(new edu.umd.clip.lm.factors.Dictionary(1));
		}
		try {
			BufferedReader reader = IO.getReader(opts.input == null ? System.in : IO.getInputStream((opts.input)));
		    for(String line = reader.readLine(); line != null; line = reader.readLine()) {
		    	parser.parseSentence(line);
		    }
		    reader.close();
		    
		    experiment.saveConfig(opts.config);
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}
